package io.afu.baseframework.dto.resp;

import io.afu.baseframework.enums.ConstantEnum;
import io.afu.baseframework.exceptions.BaseException;

import java.util.List;

/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
public class BaseResp <T> {



    private String responseFlag;

    private String responseMessage;

    private T data;


    public String getResponseFlag() {
        return responseFlag;
    }

    public void setResponseFlag(String responseFlag) {
        this.responseFlag = responseFlag;
    }

    public String getResponseMessage() {
        return responseMessage;
    }

    public void setResponseMessage(String responseMessage) {
        this.responseMessage = responseMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void makeTrueMsg(String msg){
        this.responseFlag = ConstantEnum.OPERATION_SUCCESS.getCode();
        this.responseMessage = msg;
    }

    public void makeErrorMsg(String msg){
        this.responseFlag = ConstantEnum.OPERATION_FAILED.getCode();
        this.responseMessage = msg;
    }

    public void makeTrue(ConstantEnum constantEnum){
        this.responseFlag =  constantEnum.getCode();
        this.responseMessage = constantEnum.getMsg();
    }

    public void makeFalse(ConstantEnum constantEnum){
        this.responseFlag = constantEnum.getCode();
        this.responseMessage = constantEnum.getMsg();
    }

    public void makeFalse(BaseException e){
        this.responseFlag = e.getCode();
        this.responseMessage = e.getMessage();
    }


    /**
     * 添加自定义fail方法
     * @param msg 消息
     * @param code 代码
     * @return 返回实体类
     */
    public static BaseResp fail(String msg,String code){
        BaseResp<String> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag(code);
        BaseResp.setResponseMessage(msg);
        return BaseResp;
    }


    public static BaseResp fail(BaseException e){
        BaseResp<String> BaseResp = new BaseResp<>();
        if (e.getCode()==null) {
            BaseResp.setResponseFlag(ConstantEnum.OPERATION_FAILED.getCode());
        }else {
            BaseResp.setResponseFlag(e.getCode());
        }
        BaseResp.setResponseMessage(e.getMessage());
        return BaseResp;
    }

    public static BaseResp fail(String msg) {
        BaseResp<String> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag("-1");
        BaseResp.setResponseMessage(msg);
        return BaseResp;
    }

    public static BaseResp fail(ConstantEnum constantEnum){
        BaseResp<String> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag(constantEnum.getCode());
        BaseResp.setResponseMessage(constantEnum.getMsg());
        return BaseResp;
    }

    public static BaseResp fail(Object o){
        BaseResp<Object> BaseResp = new BaseResp<>();
        BaseResp.setData(o);
        BaseResp.setResponseFlag(ConstantEnum.OPERATION_FAILED.getMsg());
        BaseResp.setResponseMessage(ConstantEnum.OPERATION_FAILED.getCode());
        return BaseResp;
    }

    public static String getExceptionMessage(Throwable e){
        if (e == null) {
            return "内部错误";
        }
        String causeMessage = e.getMessage();
        Throwable cause = e.getCause();
        if (cause != null){
            if (causeMessage == null) {
                causeMessage  = "";
            }
            causeMessage = BaseResp.getExceptionMessage(e.getCause());
        }
        return causeMessage;
    }

    public static BaseResp fail(Exception e){
        BaseResp<String> BaseResp = new BaseResp<>();
        String data = BaseResp.getExceptionMessage(e.getCause());
        BaseResp.setResponseMessage(data);
        BaseResp.setResponseFlag(ConstantEnum.OPERATION_FAILED.getCode());
        return BaseResp;
    }

    public static BaseResp fail(List<Object> objects){
        BaseResp<List<Object>> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag(ConstantEnum.OPERATION_FAILED.getCode());
        BaseResp.setResponseMessage(ConstantEnum.OPERATION_FAILED.getMsg());
        BaseResp.setData(objects);
        return BaseResp;
    }

    public static BaseResp success(){
        BaseResp<String> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag(ConstantEnum.OPERATION_SUCCESS.getCode());
        BaseResp.setResponseMessage(ConstantEnum.OPERATION_SUCCESS.getMsg());
        return BaseResp;
    }

    public static BaseResp success(String data){
        BaseResp<String> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag(ConstantEnum.OPERATION_SUCCESS.getCode());
        BaseResp.setResponseMessage(ConstantEnum.OPERATION_SUCCESS.getMsg());
        BaseResp.setData(data);
        return BaseResp;
    }

    /**
     * 通过泛型来进行参数的返回的解析
     * @param k 泛型传入的data
     * @param <K> 泛型定义传入的data的类型
     * @return 返回返回实体类
     */
    public static <K> BaseResp success(K k){
        BaseResp<K> BaseResp = new BaseResp<>();
        BaseResp.setResponseFlag(ConstantEnum.OPERATION_SUCCESS.getCode());
        BaseResp.setResponseMessage(ConstantEnum.OPERATION_SUCCESS.getMsg());
        BaseResp.setData(k);
        return BaseResp;
    }


    public boolean hasSuccess() {
        return responseFlag.equals(ConstantEnum.OPERATION_SUCCESS.getCode());
    }





}
